#!/bin/sh -f
# $Id: update-repo.sh b3a8aba613bc 2013/04/26 05:38:41 pgurumur $
# Copyright (c) 2013 Prabhu Gurumurthy <pgurumur@gmail.com>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
# File update-repo.sh generated on Monday March 25, 2013 @22:51:37 PDT
#
# __version__  = "$Revision: b3a8aba613bc $"
# __author__   = "$Author: pgurumur $"
# __modified__ = "$Date: 2013-04-26 05:38:41Z $"

PROG=${0##*/}
UID=`id -u`

usage () {
   echo "${PROG} options"
   echo "options: "
   echo " -r [ repository to be updated ]"
   echo " -l [ location of repository ]"
   echo " -h [ print this help ]"
   exit 1
}

error () {
   echo "${PROG}: $*"
   exit 1
}

[ $# -lt 1 ] && usage
while getopts c:r:l:h arg; do
   case ${arg} in
      c)
         CONFIG=${OPTARG}
         ;;

      r)
         REPO=${OPTARG}
         ;;

      l)
         LOCATION=${OPTARG}
         ;;

      h)
         usage
         ;;

      *)
         usage
         ;;
   esac
done

[ -z ${CONFIG} ] || [ -z ${REPO} ] && error "no config and/or repo defined"
LOCATION=`egrep ${REPO} ${CONFIG} | awk -F'=' '{ split($2, foo, ","); loc = foo[1]; sub(/ { /, "", loc); print loc; }'`
RTYPE=`egrep ${REPO} ${CONFIG} | awk -F'=' '{ split($2, foo, ","); loc = foo[2]; sub(/ }/, "", loc); print loc; }'`

[ ${UID} -ne 0 ] && error "root user required!"
LOCATION=`echo ${LOCATION} | awk '{ gsub(/ +/, "", $0); print $0; }'`
RTYPE=`echo ${RTYPE} | awk '{ gsub(/ +/, "", $0); print $0; }'`
echo "Location: ${LOCATION}"
echo "repo type: ${RTYPE}"

if [ -d ${LOCATION} ]; then
   chdir ${LOCATION}
   [ "$RTYPE" = "cvs" ] && cvs -q update .; exit 0
   [ "$RTYPE" = "svn" ] && svn -q update .; exit 0
   [ "$RTYPE" = "git" ] && git pull -q .; exit 0
   [ "$RTYPE" = "hg" ] && hg -q pull; hg update .; exit 0
fi
